#include <core.hpp>
#include <baseInit.hpp>

#include "stm32f4xx_hal.h"
#include <project_config.h>

#include "lcd/lt768.hpp"
#include "touch/ft4x46.hpp"
#include "touch/ft6336.hpp"
#ifdef ENABLE_GUI
#include <bsp/lcd/st7796.hpp>
#include <gui.hpp>
#include <ui.hpp>

#endif


#ifdef ENABLE_FREERTOS
#include "cmsis_os2.h"

#endif

int main()
{
    /*基础初始化*/
    core::baseInit::init();// 基础驱动初始化

#ifdef ENABLE_GUI
#ifdef ENABLE_DMA_SPI
    // 启用DMA
    using ST7796TFT = bsp::lcd::LCDController<HAL_Delay, bsp::lcd::DeviceType::ST7796, bsp::lcd::InterfaceType::SPI_RS, bsp::lcd::DMAConfigType::enable>;
#else
    // 未启用DMA
    using ST7796TFT = bsp::lcd::LCDController<HAL_Delay, bsp::lcd::DeviceType::ST7796, bsp::lcd::InterfaceType::SPI_RS, bsp::lcd::DMAConfigType::disable>;
    using LT7680ATFT = bsp::lcd::LCDController<HAL_Delay, bsp::lcd::DeviceType::LT7680A, bsp::lcd::InterfaceType::SPI, bsp::lcd::DMAConfigType::disable>;
#endif

    #ifdef ENABLE_TOUCH_OFF
    // 关闭触摸功能
    gui::control::init<gui::ui,bsp::lcd::LT768>();
#else
    // 启用触摸功能
    gui::control::init<gui::ui,bsp::lcd::LT768, bsp::touch::FT4x46>();
#endif
#endif

    core::control::init();

#ifdef ENABLE_FREERTOS
    /*主事件循环或调度器*/
    // 创建按键线程
    // const osThreadAttr_t keyTask_attributes = {
    //         .name = "keyTask",
    //         .stack_size = 256 * 4,
    //         .priority = (osPriority_t) osPriorityNormal,
    // };
    // osThreadNew([](void *) {
    //     for (;;) {
    //         PlatformKey::poll();
    //     }
    // },nullptr, &keyTask_attributes);

    // 创建后台线程
    constexpr osThreadAttr_t coreTask_attributes = {
            .name = "coreTask",
            .stack_size = 728 * 4,
            .priority = (osPriorityNormal),
    };
    osThreadNew([](void *) {
        for (;;)
        {
            core::control::handler();
        }
    },nullptr, &coreTask_attributes);

    // 创建GUI线程
#ifdef ENABLE_GUI
    constexpr osThreadAttr_t GUITask_attributes = {
            .name = "GUITask",
            .stack_size = 1024 * 4,
            .priority = (osPriorityNormal),
    };
    osThreadNew([](void *) {
        for (;;)
        {
            core::delay::ms(10);
            gui::control::handler();
        }
    },
                nullptr, &GUITask_attributes);
#endif

    // 启动调度器
    osKernelStart();
#else
    for (;;)
    {
#ifdef ENABLE_GUI
        gui::control::handler();
#endif
        core::control::handler();
    }
#endif
}